package com.ddstation.common.property;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

public class GlobalExceptionAlarmProperties {
	
	private static String fileName = "/global_exception_alarm.properties";
	
	public static void initialize(String rootpath) throws IOException {
		Properties prop =  new  Properties();
		InputStream in = null;
		try {
			in = new FileInputStream(rootpath + fileName);
			prop.load(in);
		} finally {
			if (in != null) {
				in.close();
			}
		}
		if(prop.containsKey("ALARM_ON")) {
			if("true".equalsIgnoreCase(prop.getProperty("ALARM_ON").trim()))
				ALARM_ON = true;
		}
		
		if(prop.containsKey("ALARM_TARGET")) {
			String target = prop.getProperty("ALARM_TARGET").trim();
			if(StringUtils.isNotBlank(target)) {
				ALARM_TARGET.addAll(Arrays.asList(target.split(",")));
			}
		}
		
		if(prop.containsKey("SERVER_NAME")) {
			SERVER_NAME = prop.getProperty("SERVER_NAME").trim();
		}
		
		in.close();
	}
	
	public static boolean ALARM_ON = false;
	public static String SERVER_NAME = null;
	public static List<String> ALARM_TARGET = new ArrayList<String>();
	
	public static String[] getAlermTarget() {
		if (ALARM_TARGET != null && ALARM_TARGET.size() > 0) {
			String[] target = new String[ALARM_TARGET.size()];
			ALARM_TARGET.toArray(target);
			return target;
		} else {
			return null;
		}
	}
	
}
